package com.sunwayworld.basemodule.common.editrecord.service.impl;

import com.sunwayworld.basemodule.common.editrecord.bean.LimsEditRecordBean;
import com.sunwayworld.basemodule.common.editrecord.dao.LimsEditRecordDao;
import com.sunwayworld.basemodule.common.editrecord.service.LimsEditRecordService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@GikamBean
public class LimsEditRecordServiceImpl implements LimsEditRecordService {

    @Autowired
    private LimsEditRecordDao limsEditRecordDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsEditRecordDao getDao() {
        return limsEditRecordDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsEditRecordBean limsEditRecord = jsonWrapper.parseUnique(LimsEditRecordBean.class);
        limsEditRecord.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsEditRecord);
        return limsEditRecord.getId();
    }

    @Override
    public Page<LimsEditRecordBean> selectLimsEditRecordsByOrderTaskId(Long orderTaskId, RestJsonWrapperBean wrapperBean) {
        MapperParameter mapperParameter = wrapperBean.extractMapFilter();
        mapperParameter.setFilter(SearchFilter.instance().match("orderTaskId", orderTaskId).filter(MatchPattern.EQ));
        return selectPagination(mapperParameter);
    }
}
